package com.jdrx.ocp.api;

import com.google.common.collect.Maps;
import com.jdrx.logger.bizlogs.anno.BizLogger;
import com.jdrx.ocp.beans.commons.SysConstant;
import com.jdrx.ocp.beans.dto.dept.*;
import com.jdrx.ocp.beans.vo.QueryDeptVO;
import com.jdrx.ocp.service.DeptService;
import com.jdrx.ocp.utils.Constants;
import com.jdrx.ocp.utils.OcpGwConstants;
import com.jdrx.platform.commons.rest.beans.dto.IdDTO;
import com.jdrx.platform.commons.rest.beans.enums.EApiStatus;
import com.jdrx.platform.commons.rest.beans.vo.ResposeVO;
import com.jdrx.platform.commons.rest.exception.BizException;
import com.jdrx.platform.commons.rest.factory.ResponseFactory;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

import java.util.Optional;

import static com.jdrx.platform.common.support.gateway.GwConstants.TRANSPARENT_USERID_FEILD;

/**
 * @Author: Huangxg
 * @Date: 2018/11/20 15:21
 */
@RestController
@RequestMapping("/api/0/dept/")
@Api(value = "部门管理" , description = "部门管理")
public class DeptApi {
    
    @Autowired
    DeptService deptService;

    @RequestMapping(value = "queryDept", method = RequestMethod.POST)
    @ApiOperation(value = "部门查询", notes = "部门查询")
    public ResposeVO queryDept(@ApiParam(required = true) @RequestBody @Valid QueryDeptDTO queryDeptDTO) throws BizException {
        return ResponseFactory.ok(deptService.queryDept(queryDeptDTO));
    }

    @RequestMapping(value = "queryDeptByPage", method = RequestMethod.POST)
    @ApiOperation(value = "部门查询", notes = "部门查询")
    public ResposeVO queryDeptByPage(@ApiParam(required = true) @RequestBody @Valid QueryDeptDTO queryDeptDTO) throws BizException {
        return ResponseFactory.ok(deptService.queryDept(queryDeptDTO));
    }

    @RequestMapping(value = "queryOne", method = RequestMethod.POST)
    @ApiOperation(value = "获取一个部门信息", notes = "获取一个部门信息")
    public ResposeVO queryOne(@ApiParam(required = true) @RequestBody @Valid IdDTO<Long> idDTO) throws BizException {
        QueryDeptVO queryDeptVO = deptService.queryOne(idDTO.getId());
        if(queryDeptVO == null){
            return ResponseFactory.ok(Maps.newHashMap());
        }
        return ResponseFactory.ok(queryDeptVO);
    }

    @RequestMapping(value = "queryByIds", method = RequestMethod.POST)
    @ApiOperation(value = "根据ID集获取部门数据", notes = "根据ID集获取部门数据")
    public ResposeVO queryByIds(@RequestBody @Valid QueryByIdsDTO queryByIdsDTO) throws BizException {
        return ResponseFactory.ok(deptService.queryByIds(queryByIdsDTO));
    }

    @RequestMapping(value = "getDeptChildren", method = RequestMethod.POST)
    @ApiOperation(value = "根据ID获取子级及本身的部门信息", notes = "根据ID获取子级及本身的部门信息")
    public ResposeVO getDeptChildren(@ApiParam(required = true) @RequestBody @Valid IdDTO<Long> idDTO) throws BizException {
        return ResponseFactory.ok(deptService.getDeptChildren(idDTO.getId()));
    }

    @RequestMapping(value = "getDeptChildrenToTree", method = RequestMethod.POST)
    @ApiOperation(value = "根据ID获取子级及本身的部门信息", notes = "根据ID获取子级及本身的部门信息")
    public ResposeVO getDeptChildrenToTree(@ApiParam(required = true) @RequestBody @Valid IdDTO<Long> idDTO) throws BizException {
        return ResponseFactory.ok(deptService.getDeptChildrenToTree(idDTO.getId()));
    }

    @RequestMapping(value = "getDeptParents", method = RequestMethod.POST)
    @ApiOperation(value = "根据ID获取本身以及其父级部门信息", notes = "根据ID获取本身以及其父级部门信息")
    public ResposeVO getDeptParents(@ApiParam(required = true) @RequestBody @Valid IdDTO<Long> idDTO) throws BizException {
        return ResponseFactory.ok(deptService.getDeptParents(idDTO.getId()));
    }

    @RequestMapping(value = "deptParentsPath", method = RequestMethod.POST)
    @ApiOperation(value = "指定部门父级路径", notes = "指定部门父级路径")
    public ResposeVO deptParentsPath(@ApiParam(required = true) @RequestBody @Valid IdDTO<Long> idDTO) throws BizException {
        return ResponseFactory.ok(deptService.deptParentsPath(idDTO.getId()));
    }

    @RequestMapping(value = "getDeptForTree", method = RequestMethod.POST)
    @ApiOperation(value = "获取机构部门树（所有）", notes = "获取机构部门树（所有）")
    public ResposeVO getDeptForTree() throws BizException {
        return ResponseFactory.ok(deptService.getDeptForTree());
    }

    @RequestMapping(value = "getOrgForTree", method = RequestMethod.POST)
    @ApiOperation(value = "获取公司下的树", notes = "获取公司下的树")
    public ResposeVO getOrgForTree(@RequestBody @Valid IdDTO<Integer> idDTO) throws BizException {
        return ResponseFactory.ok(deptService.getOrgForTree(idDTO.getId()));
    }

    @RequestMapping(value = "getCompanyForTree", method = RequestMethod.POST)
    @ApiOperation(value = "获取公司树", notes = "获取公司树")
    public ResposeVO getCompanyForTree(@RequestBody DeptIdDTO deptIdDTO) throws BizException {
        return ResponseFactory.ok(deptService.getCompanyForTree(deptIdDTO.getDeptId()));
    }

    @RequestMapping(value = "create",method = RequestMethod.POST)
    @ApiOperation(value = "添加部门" , notes = "添加部门")
    //@BizLogger(description = "添加部门",serviceId = SysConstant.APPLICATION_NAME)
    public ResposeVO createDept(@RequestBody @Valid CreateDeptDTO createDeptDTO,
                                @RequestHeader(TRANSPARENT_USERID_FEILD) Integer uId,
                                @RequestHeader(value = Constants.TENANT_ID, required = false, defaultValue = "") String tid) throws BizException {
        createDeptDTO.setType(Optional.ofNullable(createDeptDTO.getType()).orElse(2));
        if(deptService.createDept(createDeptDTO,uId , tid) > 0) {
            return ResponseFactory.ok("操作成功");
        }else{
            return ResponseFactory.err("添加失败",EApiStatus.ERR_SYS);
        }
    }

    @RequestMapping(value = "update",method = RequestMethod.POST)
    @ApiOperation(value = "更新部门" , notes = "更新部门")
    //@BizLogger(description = "更新部门",serviceId = SysConstant.APPLICATION_NAME)
    public ResposeVO updateDept(@RequestBody @Valid UpdateDeptDTO updateDeptDTO,
                                @RequestHeader(TRANSPARENT_USERID_FEILD) Integer uId,
                                @RequestHeader(value = Constants.TENANT_ID, required = false, defaultValue = "") String tid) throws BizException {
        if(deptService.updateDept(updateDeptDTO,uId,tid) >= 0) {
            return ResponseFactory.ok("操作成功");
        }else{
            return ResponseFactory.err("更新失败",EApiStatus.ERR_SYS);
        }
    }

    @RequestMapping(value = "delete",method = RequestMethod.POST)
    @ApiOperation(value = "删除部门" , notes = "删除部门")
    //@BizLogger(description = "删除部门",serviceId = SysConstant.APPLICATION_NAME)
    public ResposeVO deleteDept(@RequestBody @Valid IdDTO<Long> idDTO,
                                @RequestHeader(TRANSPARENT_USERID_FEILD) Integer uId) throws BizException {
        return ResponseFactory.ok(deptService.deleteDept(idDTO , uId));
    }

    @RequestMapping(value = "getDeptAllParents", method = RequestMethod.POST)
    @ApiOperation(value = "根据ID集获取本身以及其父级部门信息", notes = "根据ID集获取本身以及其父级部门信息")
    public ResposeVO getDeptAllParents(@ApiParam(required = true) @RequestBody @Valid QueryByIdsDTO idsDTO) throws BizException {
        return ResponseFactory.ok(deptService.getDeptAllParents(idsDTO.getIds()));
    }

    @RequestMapping(value = "queryDeptUserManager", method = RequestMethod.POST)
    @ApiOperation(value = "根据部门名称查询部门负责人", notes = "根据部门名称查询部门负责人")
    public ResposeVO queryDeptUserManager(@ApiParam(required = true) @RequestBody @Valid IdDTO idDTO) throws BizException {
        return ResponseFactory.ok(deptService.queryDeptUserManager(String.valueOf(idDTO.getId())));
    }

    @RequestMapping(value = "queryDeptAndUserManagerList", method = RequestMethod.POST)
    @ApiOperation(value = "查询部门及部门负责人列表", notes = "查询部门及部门负责人列表")
    public ResposeVO queryDeptAndUserManagerList() throws BizException {
        return ResponseFactory.ok(deptService.queryDeptAndUserManagerList());
    }

    @RequestMapping(value = "storageRedis", method = RequestMethod.POST)
    @ApiOperation(value = "缓存部门机构信息", notes = "缓存部门机构信息")
    public ResposeVO storageRedis() throws BizException {
        deptService.storageRedisDept();
        return ResponseFactory.ok("ok");
    }

    @RequestMapping(value = "queryCurrentDepts", method = RequestMethod.POST)
    @ApiOperation(value = "获取当前登录用户的所有部门列表", notes = "获取当前登录用户的所有部门列表")
    public ResposeVO queryCurrentDepts(@RequestHeader(value = OcpGwConstants.TRANSPARENT_OID_FEILD, required = true) Long oid) throws BizException {
        return ResponseFactory.ok(deptService.queryCurrentDepts(oid));
    }

    @RequestMapping(value = "queryDeptUserByDeptName", method = RequestMethod.POST)
    @ApiOperation(value = "根据部门名称查询部门用户列表（含子部门用户）", notes = "根据部门名称查询部门用户列表（含子部门用户）")
    public ResposeVO queryDeptUserByDeptName(@ApiParam(required = true) @RequestBody @Valid IdDTO idDTO) throws BizException {
        return ResponseFactory.ok(deptService.queryDeptUserByDeptName(String.valueOf(idDTO.getId())));
    }

    @RequestMapping(value = "queryDeptUserManagerList", method = RequestMethod.POST)
    @ApiOperation(value = "根据部门名称查询部门负责人列表", notes = "根据部门名称查询部门负责人列表")
    public ResposeVO queryDeptUserManagerList(@ApiParam(required = true) @RequestBody @Valid IdDTO idDTO) throws BizException {
        return ResponseFactory.ok(deptService.queryDeptUserManagerList(String.valueOf(idDTO.getId())));
    }

}
